package com.jck.data.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;

import com.jck.data.task.StartEtlInitTaskUtil;
import com.jck.data.util.KettleExecuUtil;
import com.jck.data.util.SpringBeanUtil;
import com.jck.data.util.Times;

/**
 * 开启tcp服务
 * 
 * @ClassName: StartDataServer
 * @Description: TODO （描述）
 * @author 作者 jacking
 * @date 2016-7-7 上午11:16:37
 */
public class StartDataServer {
	private static Logger logger = LoggerFactory.getLogger(StartDataServer.class);
	public static SpringBeanUtil sb= SpringBeanUtil.getInstance();
	public static Properties property = sb.getProperty();
	 
	public static String localip ="";
	public static String localname ="";
	static{
		 String [] inetAddress=getInetAddress();
		 if(null!=inetAddress&&inetAddress.length>1){
			 localname=inetAddress[0];
			 localip=inetAddress[1];
		 }
	}

	public static void main(String[] args) {
		try {
			
			
			/* KettleExecuUtil.getInstance().runTrans("C:\\Users\\Administrator\\Desktop\\funew数据同步ktr\\mysql-Local测试.ktr"); */
		 	/*KettleExecuUtil.getInstance().runJob("D:/masterdata-job-直接运行同步-重复间隔2分钟.kjb");*/
//			QuartzFactoryUtil.getInstance().addJobByCalendar(DataSyncTask.defaultJobName, DataSyncTask.defaultJobName, DataSyncTask.class, 2L, null);
			System.out.println("开始处理");
			logger.info("开始处理");
			if (args.length > 0) {
				if(args[0].equals("-help")) {
					System.out.println("-f -job d:/test.ktr   执行单个任务（可以支持循环）");
					System.out.println("-f d:/test.ktr   执行单个任务（不支持循环）");
					return ;
				}
				if(args.length>1&&args[0].equals("-f")) {
					if(args[1].endsWith(".ktr")) {
						KettleExecuUtil.getInstance().runTrans(args[0]);
					}else if(args[1].endsWith(".kjb")){
						KettleExecuUtil.getInstance().runJob(args[0]);
					}
				}else if(args.length>2&&args[0].equals("-f")&&args[1].equals("-job")) {
					if(args[2].endsWith(".ktr")) {
						KettleExecuUtil.getInstance().runTrans(args[0]);
					}else if(args[2].endsWith(".kjb")){
						KettleExecuUtil.getInstance().runJob(args[0]);
					}
					System.in.read();
				}
				
			}else {
				StartEtlInitTaskUtil.runInitFile();
				System.in.read();
			}
		/*	Resource re=sb.getCtx().getResource("/etlFile/masterdata全局表数据同步-直接运行.ktr");
			InputStream in=re.getInputStream();
			if(in!=null) {
				
				KettleExecuUtil.getInstance().runTrans(null,in);
			}*/
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	/*	if (args.length > 0) {
			nettyPort = args[0];
			port=Integer.parseInt(nettyPort);
		}
		
		// 开启Netty服务器线程
		DataServer server = new DataServer(sb);
		logger.error("TCP服务器启动成功--ip/端口："+localip+"/"+port+";时间："+Times.getTimes());
		System.out.println("TCP服务器启动成功--ip/端口："+ localip+"/"+ port+";时间："+Times.getTimes());
		server.bind(port);*/

	}
	public static String [] getInetAddress(){
		String [] data={};
		InetAddress ia=null;
		 String localname="";
        String localip="";
       try {
           ia=ia.getLocalHost();
            localname=ia.getHostName();
            localip=ia.getHostAddress();
            data=new String[]{localname,localip};
            return data;
       } catch (Exception e) {
    	   System.out.println("获取本机ip失败：" + Times.getTimes() );
    	   logger.error("获取本机ip失败：" + Times.getTimes());
           e.printStackTrace();
           return null;
       }
		
	}

}